
// consul.js
const Consul = require('consul');
var config = require("./util/config")

class ConsulConfig {
    constructor () {
        const serviceName = 'RiskAssessor(Version.js)';

        // 初始化 consul
        this.consul = new Consul({
            host: '127.0.0.1',
            port: 8500,
            promisify: true,
        });

        // 服务注册与健康检查配置
        this.consul.agent.service.register({
            name: serviceName,
            address: '127.0.0.1', // 注意：192.168.20.193 为我本地的内网 ip，通过 ifconfig 查看
            port: config.RiskPort,
            check: {
                http: 'http://127.0.0.1:8096/api/health',
                interval: '10s',
                timeout: '5s',
            }
        }, function(err, result) {
            if (err) {
                console.error(err);
                throw err;
            }

            console.log(serviceName + ' 注册成功！');
        })
    }
}

module.exports = ConsulConfig;